package com.yf.tz.controller;

import java.util.List;

import com.yf.tz.entity.MallSkuItemEntity;
import com.yf.tz.service.MallSkuService;
import com.yf.tz.service.MallTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import com.yf.consts.DaoConst;
import com.yf.tz.entity.MallGoodsEntity;
import com.yf.tz.service.MallGoodsService;
import com.yf.response.R;


/**
 * 产品Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Jul 01 16:32:18 CST 2020
 */
@RestController
@RequestMapping("/mall/goods")
public class MallGoodsController {
    @Autowired
    private MallGoodsService mallGoodsService;

    @Autowired
    private MallTypeService mallTypeService;

    @Autowired
    private MallSkuService skuService;


    /**
     * 列表产品
     */
    @SysLog("查看产品")
    @RequestMapping("/list")
    @RequiresPermissions("mall:goods:list")
    public R list(@RequestParam Map<String,Object> params) {
        PageHelper.startPage(DaoConst.getPage(params),DaoConst.getLIMIT(params));
        List<MallGoodsEntity> mallGoodsList = mallGoodsService.queryList(params);
        return R.ok(new PageInfo(mallGoodsList));
    }

    /**
     * 查看产品
     */
    @SysLog("查看产品")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:goods:info")
    public R info(@PathVariable("id") Long id) {
        MallGoodsEntity mallGoods = mallGoodsService.get(id);
        List<Long> typeIds = mallTypeService.findByGoods(mallGoods.getId());
        mallGoods.setTypeIds(typeIds);
        mallGoods.setSkuItemList(skuService.findSkuItemByGoodsId(id));
        mallGoods.setSkuList(skuService.findSkuByGoodsId(id));
        return R.ok(mallGoods);
    }

    /**
     * 保存产品
     */
    @SysLog("保存产品")
    @RequestMapping("/save")
    @RequiresPermissions("mall:goods:save")
    public R save(@RequestBody MallGoodsEntity mallGoods) {
        mallGoodsService.save(mallGoods);
        mallTypeService.updateByList(mallGoods.getId(),mallGoods.getTypeIds());
        skuService.resetSku(mallGoods.getId(),mallGoods.getSkuItemList(),mallGoods.getSkuList());
        return R.ok(mallGoods);
    }

    /**
     * 修改产品
     */
    @SysLog("修改产品")
    @RequestMapping("/update")
    @RequiresPermissions("mall:goods:update")
    public R update(@RequestBody MallGoodsEntity mallGoods) {
        mallGoodsService.update(mallGoods);
        mallTypeService.updateByList(mallGoods.getId(),mallGoods.getTypeIds());
        skuService.resetSku(mallGoods.getId(),mallGoods.getSkuItemList(),mallGoods.getSkuList());
        return R.ok(mallGoods);
    }

    /**
     * 删除产品
     */
    @SysLog("删除产品")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:goods:delete")
    public R delete(Long[]ids) {
        mallGoodsService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有产品
     */
    @SysLog("所有产品")
    @RequestMapping("/all")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<MallGoodsEntity> mallGoodsList = mallGoodsService.queryList(params);

        return R.ok(mallGoodsList);
    }

    /***********自定义开始***********/
}
